<?php

namespace DatMoment\VideoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use DatMoment\VideoBundle\Metier\Operations;
//use Symfony\Component\HttpFoundation\RedirectResponse;

require_once ('/VideoUrl.php');

class VideoController extends Controller {
    
    private $nbVideosByPage = 16; // nombre de videos par page.

    public function testAction() {
        $content = $this->get('templating')->render('DatMomentVideoBundle:Content:home.html.twig');
        return new Response($content);
    }

    
    public function informationsURLAction() {
        $infosVideo = getVideoInfo("http://www.dailymotion.com/video/x28hn6t_quand-manuel-valls-rencontre-zahia-a-la-fiac-zapping-actu-du-23-10-2014_news");
        $content = $this->get('templating')->render('DatMomentVideoBundle:Default:test.html.twig', array('infosVideo' => $infosVideo));
        return new Response($content);
    }

    public function viewVideoAction($pmessage = null) {
        $doctrine = $this->getDoctrine();
        $entityManager = $doctrine->getManager();

        $categorieOP = new Operations\CategorieOperations($entityManager);
        $jeuxOP = new Operations\JeuxOperations($entityManager);
        
        $content = $this->get('templating')->render('DatMomentVideoBundle:Default:formVideo.html.twig', array('listCategories' => $categorieOP->findAllCategorie(),
            'listJeux' => $jeuxOP->findAllJeux(),
            'message' => $pmessage));

        return new Response($content);
    }

    public function addVideoAction() {
        $doctrine = $this->getDoctrine();
        $entityManager = $doctrine->getManager();

        if (!empty($_POST['urlVideo'])) {
            $videoOP = new Operations\VideoOperations($entityManager);

            $urlVideo = $_POST['urlVideo'];
            $categoriesSelect = $_POST['selectCategories'];
            $jeuSelect = $_POST['selectJeu'];
            $user = $this->container->get('security.context')->getToken()->getUser();
            $infosVideo = getVideoInfo($urlVideo);

            if ($videoOP->checkIdVideo($infosVideo['id'])) {
                $message = 'video deja existante dans la base de données';
                return $this->viewVideoAction($message);
                //$url = $this->get('router')->generate('dat_moment_video_viewVideo', array('pmessage' => $message));
                //return new RedirectResponse($url);
            } else {
                if ($jeuSelect == 'Autre') {
                    $videoOP->createVideo($infosVideo['id'], $infosVideo['titre'], $infosVideo['type'], $infosVideo['description'], $infosVideo['img'], $urlVideo, $infosVideo['code'], $categoriesSelect, $user);
                } else {
                    $videoOP->createVideo($infosVideo['id'], $infosVideo['titre'], $infosVideo['type'], $infosVideo['description'], $infosVideo['img'], $urlVideo, $infosVideo['code'], $categoriesSelect, $user, $jeuSelect);
                }
                $videoOP->persist();
                $content = $this->get('templating')->render('DatMomentVideoBundle:Default:objectVideo.html.twig', array('video' => $videoOP->getVideo()));
                return new Response($content);
            }
        } else {
            $message = "impossible d'envoyer le formulaire";
            return $this->viewVideoAction($message);
            //return $this->redirect($this->generateUrl('dat_moment_video_viewVideo', array('message' => $message)));
            
        }
    }

    
    public function homeAction($numPage) {
        $doctrine = $this->getDoctrine();
        $entityManager = $doctrine->getManager();

        $videoOP = new Operations\VideoOperations($entityManager);
        $listVideos = $videoOP->getVideosByPage($this->nbVideosByPage, $numPage);
        $nbVideos = $videoOP->getNbVideos();
        $nbPages = ceil($nbVideos / $this->nbVideosByPage);

        $content = $this->get('templating')->render('DatMomentVideoBundle:Content:home.html.twig', array('listVideos' => $listVideos,
            'numPage' => $numPage,
            'nbPages' => $nbPages));
        return new Response($content);
    }
    
    
    public function categoryAction($nomCategorie, $numPage) {
        $doctrine = $this->getDoctrine();
        $entityManager = $doctrine->getManager();
        
        $videoOP = new Operations\VideoOperations($entityManager);
        $listVideos = $videoOP->getVideosByCategory($this->nbVideosByPage, $nomCategorie, $numPage);
        $nbVideos = $videoOP->getNbVideos();
        $nbPages = ceil($nbVideos / $this->nbVideosByPage);
        
        $content = $this->get('templating')->render('DatMomentVideoBundle:Content:home.html.twig', array('listVideos' => $listVideos,
            'numPage' => $numPage,
            'nbPages' => $nbPages));
        return new Response($content);
    }
    
    
    public function gameAction($nomJeu, $numPage) {
        $doctrine = $this->getDoctrine();
        $entityManager = $doctrine->getManager();
        
        $videoOP = new Operations\VideoOperations($entityManager);
        $listVideos = $videoOP->getVideosByGame($this->nbVideosByPage, $nomJeu, $numPage);
        $nbVideos = $videoOP->getNbVideos();
        $nbPages = ceil($nbVideos / $this->nbVideosByPage);
        
        $content = $this->get('templating')->render('DatMomentVideoBundle:Content:home.html.twig', array('listVideos' => $listVideos,
            'numPage' => $numPage,
            'nbPages' => $nbPages));
        return new Response($content);
    }
    
    public function randomAction() {
        $doctrine = $this->getDoctrine();
        $entityManager = $doctrine->getManager();
        
        $videoOP = new Operations\VideoOperations($entityManager);
        $nbVideos = $videoOP->getNbVideos();
        $random = rand(1, $nbVideos);
        $video = $videoOP->getVideoById($random);
        while ($video == null) {
            $random = rand(1, $nbVideos);
            $video = $videoOP->getVideoById($random);
        }
        
        $content = $this->get('templating')->render('DatMomentVideoBundle:Default:objectVideo.html.twig', array('video' => $video));
        return new Response($content);
    }
    
    
    public function videoAction($idVideo) {
        $doctrine = $this->getDoctrine();
        $entityManager = $doctrine->getManager();
        
        $videoOP = new Operations\VideoOperations($entityManager);
        $video = $videoOP->getVideoByIdVIdeo($idVideo);         
        
        $content = $this->get('templating')->render('DatMomentVideoBundle:Default:objectVideo.html.twig', array('video' => $video));
        return new Response($content);
    }

}
